package mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Mysql {
	
	Connection con;
	
	public Mysql() {
		try {
			con= DriverManager.getConnection("jdbc:mysql://localhost/blade", "root", "");
			//con= DriverManager.getConnection("jdbc:mysql://mysql.agenciaweber.com.br/agenciaweber18", "agenciaweber18", "game123");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println("Conectado!");		
	}
	
	
	
	public ResultSet insertUpdate(String strsql) {
		try {
			Statement stm = this.con.createStatement();
			if (strsql.toLowerCase().subSequence(0, 6).equals("insert")) {
				stm.executeUpdate(strsql, Statement.RETURN_GENERATED_KEYS);	
				return stm.getGeneratedKeys();
			}
			else {
				stm.executeUpdate(strsql);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//System.out.print("0");
		}
		return null;		
	}
	
	public ResultSet select(String strsql) {
		try {
			Statement stm = this.con.createStatement();
			return stm.executeQuery(strsql);	
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;	
	}
	
	public void close() {		
		try {
			this.con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}				
	}
	
}
